package be.gnx.fogo.application.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;

import be.gnx.fogo.application.util.HibernateUtil;

public class ObjectDao<Type, PrimaryKey extends Serializable> {

  protected Class<Type> classType;

  protected String keyName;

  public ObjectDao(Class<Type> classType, String keyName) {
    this.classType = classType;
    this.keyName = keyName;
  }

  //public void persist(Type object) {
  //  HibernateUtil.getCurrentSession().persist(object);
  //}

  public PrimaryKey save(Type object) {
    return (PrimaryKey) HibernateUtil.getCurrentSession().save(object);
  }

  public Type get(PrimaryKey id) {
    return (Type) HibernateUtil.getCurrentSession().get(classType, id);
  }

  public void update(Type object) {
    HibernateUtil.getCurrentSession().update(object);
  }

  public void delete(Type object) {
    HibernateUtil.getCurrentSession().delete(object);
  }

  public List<Type> getAll() {
    Query query = HibernateUtil.createQuery("from " + classType.getCanonicalName() + " order by " +  keyName);
    return (List<Type>) query.list();
  }

}